import Http from '../../../utils/http.js';
var WxParse = require('../../../utils/wxParse/wxParse.js');
let app = getApp();
var Moment = require("../../../utils/moment.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityCode:'100093',//当前商品code
    current: 0, // 索引值
    loading:false,
    attributes:[],
    commodityCommon:{},
    activeTab:'detail',
    evalutionLoading:false,//评论加载
    evalutionLoadingComplete:false,//评论数据加载完毕
    evalutionPage:1,//评论当前分页
    evalutionList:[],
    checkInDate: Moment(new Date()).format('YYYY-MM-DD'),
    checkOutDate: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD'),
    checkInWeek: Moment(new Date()).getWeek(Moment(new Date()).format('YYYY-MM-DD')),
    checkOutWeek: Moment(new Date()).getWeek(Moment(new Date()).add(1, 'day').format('YYYY-MM-DD')),
    totalNight:1,//共几晚
    soldOutRoomList:[],//已售罄的房间
    isSellOut:false,//是否当天已售罄
    showFacilityModel:false,//酒店设施详情
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      commodityCode: options.commodityCode ||'100093',
      isSellOut: options.isTodaySellOut=='true'
    })
    this.getCommodityDetail();
    this.getCommentList();
  },
  //获取评论数据
  getCommentList(){
    Http.Request('GET', `/api/customer/commodity/getCommodityCommentByCode/${this.data.commodityCode}/v1?page=${this.data.evalutionPage}&size=${app.globalData.pageSize}`,).then(res=>{
      //返回数组为空或者返回的条数小于设定的size认为所有数据已经加载完成
      if (res && (!res.length || res.length < app.globalData.pageSize)){
        this.setData({
          evalutionLoadingComplete : true
        })
      }
      this.setData({
        evalutionList: this.data.evalutionList.concat(res),
        evalutionLoading:false
      })
    }).catch(err=>{

    })
  },
  //获取商品详情
  getCommodityDetail(){
    this.setData({
      loading: true
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    Http.Request('GET', `/api/customer/commodity/getCommodityByCode/${this.data.commodityCode}/v1`).then(res=>{
      let attributes =[];
      const facilityType = app.globalData.dictionary.custFacilityType;
      let keysList = Object.keys(res.attributes);
      keysList.forEach((o,i)=>{
        const node = facilityType.find(f=>{
          return o===f.name
        });
        if(node){
          node.srcColor = `/images/${node.name}_COLOR.png`;
          node.src = `/images/${node.name}.png`;
          node.attr = res.attributes[o]
        }
        attributes.push(node)
      })
      this.setData({
        attributes: attributes,
        commodityCommon: res.commodityCommon,
        loading: false
      })
      WxParse.wxParse('commodityDetail', 'html', this.data.commodityCommon.commodityDetail || app.globalData.defaultHtml, this, 25);
      
    }).catch(err=>{
      wx.hideLoading();
      app.globalFn.reLoad(this.getCommodityDetail, err)
    })
  },
  //显示弹窗
  showModal(){
    this.selectComponent("#maskModals").showChoose()
  },
  //轮播图切换
  swiperChange(e){
    const current = e.detail.current;
    this.setData({
      current
    })
  },
  //切换详情、评价
  tabSwitch(e){
    const activeTab = e.currentTarget.dataset.type;
    this.setData({
      activeTab
    })
  },
  //滑动到底部
  scrollLower(){
    if (this.data.evalutionLoading || this.data.evalutionLoadingComplete) return;
    this.setData({
      evalutionLoading:true,
      evalutionPage: this.data.evalutionPage+1
    })
    this.getCommentList()
    
  },
  //获取房态
  getRoomState(params){
    params = params.detail;
    Http.Request('GET', `/api/customer/commodity/roomInfo/${this.data.commodityCode}/${params.beginDate}/${params.days}/v1`).then(res=>{
      //找到关闭房间
      const soldOutRoomList = res.filter(o=>{
        return o.roomState.name==='CLOSE'
      })
      const statePrice = 'commodityCommon.commodityPrice'
      this.setData({
        soldOutRoomList: soldOutRoomList,
        roomStateList:res,
        [statePrice]:res[0].roomPrice,
      });
      wx.hideLoading();
      this.selectComponent("#calendarChoose").setSoldOutData();
    })
  },
  //确认日期选择（子组件事件）
  handleDateConfirm(params){
    params = params.detail;
    const statePrice = 'commodityCommon.commodityPrice';
    const roomPrice = this.data.roomStateList.find(o=>{
      return o.date === params.checkInDate
    }).roomPrice
    this.setData({
      ...(params),
      [statePrice]: roomPrice

    });
    this.selectComponent("#maskModals").hideModal();
  },
  //立即预订
  handleBookNow(){
    if(this.data.isSellOut){
      this.showModal();
      return
    }
    wx.navigateTo({
      url: `./createOrder/index?checkInDate=${this.data.checkInDate}&checkOutDate=${this.data.checkOutDate}&totalNight=${this.data.totalNight}&commoditySellingPoint=${this.data.commodityCommon.commoditySellingPoint}&commodityName=${this.data.commodityCommon.commodityName}&commodityCode=${this.data.commodityCode}&commodityImage=${this.data.commodityCommon.commodityImage[0]}`
    })
  },
  //显示酒店设施详情
  comAttributesClick(){
    let isShow = !this.data.showFacilityModel;
    this.setData({
      showFacilityModel: isShow
    })
  }
})